﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace ddb_BT1
{
    public partial class frmHoaDonBanHangFinal : DevExpress.XtraEditors.XtraForm
    {
        public frmHoaDonBanHangFinal()
        {
            InitializeComponent();
             STT = (int)pHIEUBANHANGTableAdapter.getSTTPhieuNhap();
            
        }
        public frmHoaDonBanHangFinal( string STT)
        {
            InitializeComponent();
            STTPhieuNhap = STT;
        }
        private string STTPhieuNhap=string.Empty;
        private int STT;
        private void frmHoaDonBanHangFinal_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dataHeThongThongTinKeToan1.CHITIET_PHIEUBANHANG' table. You can move, or remove it, as needed.
          
            // TODO: This line of code loads data into the 'dataHeThongThongTinKeToan1.NHANVIEN' table. You can move, or remove it, as needed.
            this.nHANVIENTableAdapter.Fill(this.dataHeThongThongTinKeToan1.NHANVIEN);
            // TODO: This line of code loads data into the 'dataHeThongThongTinKeToan1.KHOHANG' table. You can move, or remove it, as needed.
            this.kHOHANGTableAdapter.Fill(this.dataHeThongThongTinKeToan1.KHOHANG);
            // TODO: This line of code loads data into the 'dataHeThongThongTinKeToan1.DOITAC' table. You can move, or remove it, as needed.
            this.dOITACTableAdapter.Fill(this.dataHeThongThongTinKeToan1.DOITAC);
    
            // TODO: This line of code loads data into the 'dataHeThongThongTinKeToan1.PHIEUBANHANG' table. You can move, or remove it, as needed.
            if (STTPhieuNhap == string.Empty)
            {
                this.pHIEUBANHANGTableAdapter.FillBySoPhieu(this.dataHeThongThongTinKeToan1.PHIEUBANHANG,txtSoPhieu.Text);
                txtSoPhieu.Text = "PN" + STT.ToString().PadLeft(4, '0');
                txtDVTKH.SelectionStart = -1;
              
            }
            else
            {
                this.pHIEUBANHANGTableAdapter.FillBySoPhieu(this.dataHeThongThongTinKeToan1.PHIEUBANHANG,STTPhieuNhap);
            }
          
            /// chi tiet hd
            if (STTPhieuNhap == string.Empty)
            {
                this.cHITIET_PHIEUBANHANGTableAdapter.FillBySoPhieu(this.dataHeThongThongTinKeToan1.CHITIET_PHIEUBANHANG,txtSoPhieu.Text);
            }
            else
            {
                this.cHITIET_PHIEUBANHANGTableAdapter.FillBySoPhieu(this.dataHeThongThongTinKeToan1.CHITIET_PHIEUBANHANG, STTPhieuNhap);
            }


          
             
        }



        private void SLUE_DVKH_EditValueChanged(object sender, EventArgs e)
        {
            
                CurrencyManager cm = BindingContext[txtDVTKH.Properties.DataSource, ""] as CurrencyManager;
                cm.Position = txtDVTKH.Properties.GetIndexByKeyValue(txtDVTKH.EditValue);
           
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (STTPhieuNhap == string.Empty)
            {
                DataRow dr = dataHeThongThongTinKeToan1.PHIEUBANHANG.NewRow();
                dr["SoPhieuBanHang"] = txtSoPhieu.Text;
                dr["NgayLapPhieu"] = txtNgayLapPhieu.Text;
                dr["SoChungTu"] = txtSoCT.Text;
                dr["NgayCT"] = txtNgayCT.Text;
                dr["TaiKhoanChinh"] = txtTKChinh.Text;
                dr["MaKhoXuatBan"] = cbbKho.Text;
                dr["MaDoiTac"] = txtDVTKH.Text;
                dr["MaNV"] = cbbNhanVien.Text;
                dr["ThueSuat"] = txtThue.Text;
                dr["HanThanhToan"] = txtNgayHachToan.Text;
                dr["SoHoaDon"] = txtSOHD.Text;
                dr["SoQuyen"] = txtSoQuyen.Text;
                dr["HanThanhToan"] = txtNgayHachToan.Text;
                dr["LyDo"] = txtLyDo.Text;
                dataHeThongThongTinKeToan1.PHIEUBANHANG.Rows.Add(dr);
                this.pHIEUBANHANGTableAdapter.Update(dataHeThongThongTinKeToan1.PHIEUBANHANG);
                STTPhieuNhap=txtSoPhieu.Text;
                //  dataHeThongThongTinKeToan1.PHIEUBANHANG.AddPHIEUBANHANGRow(this.pHIEUBANHANGBindingSource.Current as DataHeThongThongTinKeToan.PHIEUBANHANGRow);

            }

            else
            {
                DataRow dr = dataHeThongThongTinKeToan1.PHIEUBANHANG.FindBySoPhieuBanHang(txtSoPhieu.Text);
                dr.BeginEdit();
                dr["SoPhieuBanHang"] = txtSoPhieu.Text;
                dr["NgayLapPhieu"] = txtNgayLapPhieu.Text;
                dr["SoChungTu"] = txtSoCT.Text;
                dr["NgayCT"] = txtNgayCT.Text;
                dr["TaiKhoanChinh"] = txtTKChinh.Text;
                dr["MaKhoXuatBan"] = cbbKho.Text;
                dr["MaDoiTac"] = txtDVTKH.Text;
                dr["MaNV"] = cbbNhanVien.Text;
                dr["ThueSuat"] = txtThue.Text;
                dr["HanThanhToan"] = txtNgayHachToan.Text;
                dr["SoHoaDon"] = txtSOHD.Text;
                dr["SoQuyen"] = txtSoQuyen.Text;
                dr["HanThanhToan"] = txtNgayHachToan.Text;
                dr["LyDo"] = txtLyDo.Text;
                dr.EndEdit();
                this.pHIEUBANHANGTableAdapter.Update(dataHeThongThongTinKeToan1.PHIEUBANHANG);
            }
            this.cHITIET_PHIEUBANHANGTableAdapter.Update(dataHeThongThongTinKeToan1.CHITIET_PHIEUBANHANG);
        }

        private void gridView2_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
                DevExpress.XtraGrid.Views.Grid.GridView view = (DevExpress.XtraGrid.Views.Grid.GridView)sender;
            view.ActiveEditor.IsModified = true;
            gridView2.SetFocusedRowCellValue("SoPhieuBanHang",txtSoPhieu.Text);
        }
    }
}